<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>接济单管理 - 监室管理系统</title>
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #3498db;
            --background: #f8f9fa;
            --accent-color: #e74c3c;
            --success-color: #2ecc71;
        }

        body {
            font-family: 'Segoe UI', system-ui, sans-serif;
            margin: 0;
            padding: 0;
            background-color: var(--background);
        }

        /* 导航栏样式 */
        .navbar {
            background-color: var(--primary-color);
            padding: 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .nav-container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }

        .logo {
            color: white;
            font-size: 1.5em;
            font-weight: bold;
            padding: 15px 0;
            text-decoration: none;
        }

        .nav-links {
            display: flex;
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .nav-links li {
            margin: 0;
            padding: 0;
        }

        .nav-links a {
            color: rgba(255,255,255,0.8);
            text-decoration: none;
            padding: 20px 15px;
            display: block;
            transition: all 0.3s;
            border-bottom: 3px solid transparent;
        }

        .nav-links a:hover, .nav-links a.active {
            color: white;
            border-bottom: 3px solid var(--secondary-color);
        }

        .user-controls {
            display: flex;
            align-items: center;
        }

        .content {
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .status-badge {
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 0.8em;
            font-weight: bold;
        }
        
        .status-draft { background-color: #f8f9fa; color: #6c757d; }
        .status-pending { background-color: #fff3cd; color: #856404; }
        .status-printed { background-color: #d1ecf1; color: #0c5460; }
        .status-sent { background-color: #cce7ff; color: #004085; }
        .status-received { background-color: #d4edda; color: #155724; }
        .status-archived { background-color: #e2e3e5; color: #383d41; }
        
        .action-btn {
            margin-right: 5px;
            margin-bottom: 5px;
        }
        
        .form-card {
            transition: transform 0.2s;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .form-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 4px 12px rgba(52,152,219,0.2);
        }
        
        .page-title {
            margin-bottom: 20px;
            color: var(--primary-color);
        }
        
        .btn-primary {
            background: var(--secondary-color);
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            color: white;
            text-decoration: none;
            display: inline-block;
            transition: background 0.3s;
        }
        
        .btn-primary:hover {
            background: #2980b9;
        }
        
        .alert {
            margin-bottom: 15px;
            padding: 10px 15px;
            border-radius: 5px;
        }
        
        .alert-info {
            background-color: #e3f2fd;
            border-left: 4px solid #3498db;
        }
        
        .alert-success {
            background-color: #e8f5e9;
            border-left: 4px solid #2ecc71;
        }
        
        .alert-danger {
            background-color: #ffebee;
            border-left: 4px solid #e74c3c;
        }
    </style>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar">
        <div class="nav-container">
            <a href="/" class="logo">监室管理系统</a>
            <ul class="nav-links">
                <li><a href="/">首页</a></li>
                <li><a href="/rooms">房间管理</a></li>
                <li><a href="/relief_forms" class="active">接济管理</a></li>
                <li><a href="/medical">医疗辅助</a></li>
                <li><a href="/record">记录审计</a></li>
            </ul>
            <div class="user-controls">
                {% if current_user.is_authenticated %}
                <span style="color: white; margin-right: 15px;">
                    欢迎，{{ current_user.username }}
                    {% if current_user.user_type != 'normal' %}
                    <span style="background: #f39c12; color: white; padding: 3px 8px; border-radius: 3px; font-size: 0.8em; margin-left: 5px;">管理员</span>
                    {% else %}
                    <span style="background: #3498db; color: white; padding: 3px 8px; border-radius: 3px; font-size: 0.8em; margin-left: 5px;">普通用户</span>
                    {% endif %}
                </span>
                <a href="/logout" style="background: var(--accent-color); color: white; padding: 8px 15px; border-radius: 5px; text-decoration: none;">
                    登出系统
                </a>
                {% else %}
                <a href="/login" style="background: var(--secondary-color); color: white; padding: 8px 15px; border-radius: 5px; text-decoration: none; margin-right: 10px;">
                    登录
                </a>
                <a href="/register" style="background: var(--success-color); color: white; padding: 8px 15px; border-radius: 5px; text-decoration: none;">
                    注册
                </a>
                {% endif %}
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <!-- 页面标题 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1>接济单管理</h1>
            {% if current_user.role_id == 2 %} <!-- 管教角色 -->
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createFormModal">
                <i class="bi bi-plus-circle"></i> 新建接济单
            </button>
            {% endif %}
        </div>

        <!-- 显示消息 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                <div class="alert alert-{{ 'danger' if category == 'error' else 'success' if category == 'success' else 'info' }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <!-- 接济单列表 -->
        {% if relief_forms %}
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>接济单编号</th>
                        <th>状态</th>
                        <th>开具人</th>
                        <th>开具时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for form in relief_forms %}
                    <tr>
                        <td>{{ form.form_id }}</td>
                        <td>
                            <span class="status-badge status-{{ form.status|lower|replace(' ', '-') }}">
                                {{ form.status }}
                            </span>
                        </td>
                        <td>{{ form.issuer_name }}</td>
                        <td>{{ form.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                        <td>
                            <div class="btn-group" role="group">
                                <a href="/relief_forms/{{ form.id }}" class="btn btn-outline-primary btn-sm action-btn">
                                    <i class="bi bi-eye"></i> 查看
                                </a>
                                
                                {% if current_user.role_id == 2 and form.status == 'draft' %} <!-- 管教角色 -->
                                <button class="btn btn-outline-warning btn-sm action-btn" 
                                        onclick="editForm({{ form.id }})">
                                    <i class="bi bi-pencil"></i> 编辑
                                </button>
                                {% endif %}
                                
                                {% if current_user.role_id == 1 and form.status == 'pending' %} <!-- 领导角色 -->
                                <button class="btn btn-outline-success btn-sm action-btn" 
                                        onclick="approveForm({{ form.id }})">
                                    <i class="bi bi-check-circle"></i> 审核通过
                                </button>
                                <button class="btn btn-outline-danger btn-sm action-btn" 
                                        onclick="rejectForm({{ form.id }})">
                                    <i class="bi bi-x-circle"></i> 审核不通过
                                </button>
                                {% endif %}
                                
                                {% if current_user.role_id == 4 and form.status == 'printed' %} <!-- 接济窗口角色 -->
                                <button class="btn btn-outline-info btn-sm action-btn" 
                                        onclick="sendForm({{ form.id }})">
                                    <i class="bi bi-send"></i> 寄出
                                </button>
                                {% endif %}
                                
                                {% if current_user.role_id == 4 and form.status == 'sent' %} <!-- 接济窗口角色 -->
                                <button class="btn btn-outline-success btn-sm action-btn" 
                                        onclick="receiveForm({{ form.id }})">
                                    <i class="bi bi-check2-square"></i> 签收
                                </button>
                                {% endif %}
                                
                                {% if current_user.role_id == 2 and form.status == 'received' %} <!-- 管教角色 -->
                                <button class="btn btn-outline-secondary btn-sm action-btn" 
                                        onclick="archiveForm({{ form.id }})">
                                    <i class="bi bi-archive"></i> 归档
                                </button>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="alert alert-info">
            <i class="bi bi-info-circle"></i> 暂无接济单记录
        </div>
        {% endif %}
    </div>

    <!-- 新建接济单模态框 -->
    <div class="modal fade" id="createFormModal" tabindex="-1" aria-labelledby="createFormModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="createFormModalLabel">新建接济单</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="createForm" action="/api/relief_forms" method="post">
                        <div class="mb-3">
                            <label for="room" class="form-label">房间号</label>
                            <select class="form-control" id="room" name="room" required>
                                <option value="">请选择房间号</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label for="recipient" class="form-label">接收人姓名</label>
                            <select class="form-control" id="recipient" name="recipient" required>
                                <option value="">请选择接收人</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label for="items" class="form-label">物品清单</label>
                            <textarea class="form-control" id="items" name="items" rows="3" required></textarea>
                        </div>
                        <div class="mb-3">
                            <label for="total_value" class="form-label">存入金额（元）</label>
                            <input type="number" class="form-control" id="total_value" name="total_value" step="0.01" min="0" placeholder="请输入存入金额">
                        </div>
                        <div class="mb-3">
                            <label for="notes" class="form-label">备注</label>
                            <textarea class="form-control" id="notes" name="notes" rows="2"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="submitForm()">创建</button>
                </div>
            </div>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script>
        // 页面加载时获取房间号列表
        $(document).ready(function() {
            // 获取房间号列表
            $.ajax({
                url: '/api/user/rooms',
                method: 'GET',
                success: function(response) {
                    const roomSelect = $('#room');
                    response.forEach(function(room) {
                        roomSelect.append(`<option value="${room}">${room}</option>`);
                    });
                },
                error: function(xhr, status, error) {
                    console.error('获取房间号失败:', error);
                }
            });
            
            // 房间号选择变化时获取人员列表
            $('#room').change(function() {
                const roomNumber = $(this).val();
                const recipientSelect = $('#recipient');
                
                // 清空原有选项
                recipientSelect.empty();
                recipientSelect.append('<option value="">请选择接收人</option>');
                
                if (roomNumber) {
                    // 获取房间内人员列表
                    $.ajax({
                        url: `/api/room/${roomNumber}/persons`,
                        method: 'GET',
                        success: function(response) {
                            response.forEach(function(person) {
                                recipientSelect.append(`<option value="${person.name}">${person.name}</option>`);
                            });
                        },
                        error: function(xhr, status, error) {
                            console.error('获取人员列表失败:', error);
                        }
                    });
                }
            });
        });
        
        // 提交新建接济单表单
        function submitForm() {
            const form = document.getElementById('createForm');
            const formData = {
                room: $('#room').val(),
                recipient: $('#recipient').val(),
                items: $('#items').val(),
                total_value: parseFloat($('#total_value').val()) || 0.0,
                notes: $('#notes').val()
            };
            
            $.ajax({
                url: '/api/relief_forms',
                method: 'POST',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function(response) {
                    location.reload();
                },
                error: function(xhr, status, error) {
                    alert('创建失败: ' + xhr.responseText);
                }
            });
        }
        
        // 审核通过接济单
        function approveForm(formId) {
            if (confirm('确定要审核通过这张接济单吗？')) {
                $.ajax({
                    url: `/api/relief_forms/${formId}/approve`,
                    method: 'PUT',
                    success: function(response) {
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        alert('操作失败: ' + xhr.responseText);
                    }
                });
            }
        }
        
        // 审核不通过接济单
        function rejectForm(formId) {
            if (confirm('确定要审核不通过这张接济单吗？')) {
                $.ajax({
                    url: `/api/relief_forms/${formId}/reject`,
                    method: 'PUT',
                    success: function(response) {
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        alert('操作失败: ' + xhr.responseText);
                    }
                });
            }
        }
        
        // 标记为已寄出
        function sendForm(formId) {
            if (confirm('确定要标记为已寄出吗？')) {
                $.ajax({
                    url: `/api/relief_forms/${formId}/send`,
                    method: 'PUT',
                    success: function(response) {
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        alert('操作失败: ' + xhr.responseText);
                    }
                });
            }
        }
        
        // 标记为已签收
        function receiveForm(formId) {
            if (confirm('确定要标记为已签收吗？')) {
                $.ajax({
                    url: `/api/relief_forms/${formId}/receive`,
                    method: 'PUT',
                    success: function(response) {
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        alert('操作失败: ' + xhr.responseText);
                    }
                });
            }
        }
        
        // 归档接济单
        function archiveForm(formId) {
            if (confirm('确定要归档这张接济单吗？')) {
                $.ajax({
                    url: `/api/relief_forms/${formId}/archive`,
                    method: 'PUT',
                    success: function(response) {
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        alert('操作失败: ' + xhr.responseText);
                    }
                });
            }
        }
    </script>
</body>
</html>